package com.company.genealogy.web.controller;

import com.company.genealogy.common.util.JwtUtil;
import com.company.genealogy.web.service.FamilyTreeService;
import com.company.genealogy.web.dto.TreeDragDTO;
import com.company.genealogy.web.dto.BatchMoveDTO;
import com.company.genealogy.web.dto.GenerationNameDTO;
import com.company.genealogy.web.vo.FamilyTreeNodeWithRelationVO;
import com.company.genealogy.web.vo.TimelineMemberVO;
import com.company.genealogy.common.result.ApiResponse;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.company.genealogy.common.constant.ApiCodeEnum;

@RestController
@RequestMapping("/api/v1/family")
public class FamilyTreeController {
    @Autowired
    private FamilyTreeService familyTreeService;

    @Autowired
    private JwtUtil jwtUtil;

    // 新增：获取家族树结构（含多配偶、长幼排序、关系类型）
    @GetMapping("/tree-with-relation")
    public ApiResponse<FamilyTreeNodeWithRelationVO> getFamilyTreeWithRelation(@RequestParam(required = false) Long clanId,
                                                       @RequestParam(required = false) Long branchId,
                                                       @RequestParam(required = false) Integer generation,
                                                       @RequestParam(required = false) Long rootMemberId,
                                                       HttpServletRequest request) {
        if (clanId == null) {
            String authHeader = request.getHeader("Authorization");
            if (authHeader != null && authHeader.startsWith("Bearer ")) {
                String token = authHeader.substring(7);
                clanId = jwtUtil.getClanIdFromToken(token);
            }
        }
        FamilyTreeNodeWithRelationVO tree = familyTreeService.getFamilyTreeWithRelation(clanId, branchId, generation, rootMemberId);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, tree);
    }

    // 2. 拖拽调整成员关系
    @PostMapping("/tree/drag")
    public ApiResponse<Void> dragMember(@RequestBody TreeDragDTO dto) {
        familyTreeService.dragMember(dto);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
    }

    // 3. 批量移动成员
    @PostMapping("/tree/batch-move")
    public ApiResponse<Void> batchMove(@RequestBody BatchMoveDTO dto) {
        familyTreeService.batchMove(dto);
        return ApiResponse.success();
    }

    // 4. 时间轴模式成员列表
    @GetMapping("/timeline")
    public ApiResponse<List<TimelineMemberVO>> getTimeline(@RequestParam Long clanId,
                                                           @RequestParam(required = false) Long branchId,
                                                           @RequestParam(required = false) Integer startYear,
                                                           @RequestParam(required = false) Integer endYear) {
        List<TimelineMemberVO> list = familyTreeService.getTimeline(clanId, branchId, startYear, endYear);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, list);
    }

    // 5. 自动排辈字生成
    @PostMapping("/generation-names")
    public ApiResponse<Void> generateGenerationNames(@RequestBody GenerationNameDTO dto) {
        familyTreeService.generateGenerationNames(dto);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
    }
} 